package hn.cch.mongodb.configuration;

import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config
        .EnableMongoRepositories;

/**
 * @author CCH
 * @version 2018.10.16
 */

@Configuration
@EnableMongoRepositories(basePackages = "hn.cch.mongodb.repository")
@PropertySource(value = "classpath:jdbc.properties")
public class MongodbConfiguration extends AbstractMongoConfiguration {



    @Autowired
    private Environment environment;

    @Override
    protected String getDatabaseName() {
        return environment.getRequiredProperty("jdbc.name");
    }

    @Override
    @Bean
    public Mongo mongo() throws Exception {
        String host = environment.getRequiredProperty("jdbc.host");
        int port = Integer.parseInt(environment.getRequiredProperty("jdbc.port"));
        ServerAddress address = new ServerAddress(host, port);
        return new MongoClient(address);
    }


}
